#include "learn.h"

static uint16_t count = 0;

_Noreturn void class_5_1()
{
    //使能GPIOB时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    //使能AFIO时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    GPIO_InitTypeDef GPIOB_Init = {
        .GPIO_Mode = GPIO_Mode_IPU,
        .GPIO_Speed = GPIO_Speed_50MHz,
        .GPIO_Pin = GPIO_Pin_14,
    };
    GPIO_Init(GPIOB, &GPIOB_Init);

    //exti 配置
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);

    EXTI_InitTypeDef EXTI_InitSt = {
        .EXTI_Line = EXTI_Line14,
        .EXTI_LineCmd = ENABLE,
        .EXTI_Trigger = EXTI_Trigger_Rising_Falling,
        .EXTI_Mode = EXTI_Mode_Interrupt,
    };
    EXTI_Init(&EXTI_InitSt);

    //nvic 配置
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitTypeDef NVIC_InitSt = {
        .NVIC_IRQChannel = EXTI15_10_IRQn,
        .NVIC_IRQChannelCmd = ENABLE,
        .NVIC_IRQChannelPreemptionPriority = 1,
        .NVIC_IRQChannelSubPriority = 1,
    };
    NVIC_Init(&NVIC_InitSt);

    OLED_Init();
    OLED_ShowString(1, 3, "Hello World!");
    OLED_ShowString(2, 1, "Count:");
    while (1) {
        OLED_ShowNum(2, 7, count, 4);
    }
}

void EXTI15_10_IRQHandler()
{
    if (EXTI_GetITStatus(EXTI_Line14) == SET) {
        count++;
        EXTI_ClearITPendingBit(EXTI_Line14);
    }
}
